/* ***************************************************** */
/* file cpn.h:  contains definitions for the cpn program */
/*                                                       */
/* Copyright (c) 1990-1996 by Donald R. Tveter           */
/*                                                       */
/* *******************************************************/

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include <math.h>
#include <string.h>
#include <time.h>

#ifdef DOS16
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define INT32 long
#define MAXINT 32767
#endif

#ifdef DOS32
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define INT32 int
#define MAXINT 2147483647
#endif

#ifdef UNIX

#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

#ifdef BSD
#include <termios.h>
#elif defined NEXT
#include <malloc.h>
#include <sgtty.h>
#else /* SysV */
#include <malloc.h>
#include <termio.h>
#endif
#define INT32 int
#define SIGINT 2
//#define CLOCKS_PER_SEC 1000000.0
#endif

#ifdef FLOAT
#define WTTYPE float
#define WTSIZE 4
#define REAL float
#else
#define WTTYPE double
#define WTSIZE 8
#define REAL double
#endif

#define PG(x) if (pg(stdout,x) == 1) return(1); else return(0);
#define DBG(x) if (debugoutput == '+') pg(stdout,x);

#define RXSTRSIZE 257     /* max length of string to store rx files */
#define MAXFORMAT 21      /* maximum number of format breaks */
#define BUFFSIZE 258      /* maximum input buffer size */
#define OUTSTRSIZE 400    /* max length of output string */
#define FILENAMESIZE 15   /* maximum length of a file name */
#define MAXFILES 10       /* maximum number of files on file stack */
#define GT 0              /* a symbol meaning > */
#define GE 1              /* a symbol meaning >= */
#define scale(x) x        /* scale not used in real version */
#define unscale(x) x      /* unscale not used in real version */
#define unscaleint(x) x   /* unscaleint not used in real version */
#define TRAIN 0           /* selects the training patterns */
#define TEST 1            /* selects the test patterns */
#define TOL 0             /* selects stats based on tolerance */
#define MAX 1             /* selects stats based on max value */

typedef struct seednode
   {
     unsigned val;            /* a seed value */
     struct seednode *next;   /* pointer to next node */
   } SEEDNODE;

typedef struct patnode
   {
    WTTYPE *addr;             /* address to get the value from */
    WTTYPE val;               /* a normal value */
    short layer;              /* layer of the net to take value from */
    short unitno;             /* unit of the layer to take value from */
   } PATNODE;

typedef struct patlist
   {
     PATNODE *pats;           /* the list of patterns */
     struct patlist *next;    /* pointer to the next pattern */
   } PATLIST;

typedef struct unit
   {
     short layernumber;       /* layer number of the unit */
     short unitnumber;        /* position within layer */
     REAL error;              /* to sum error factors */
     WTTYPE oj;               /* state of activation of node */
     WTTYPE tj;               /* a temporary variable */
     struct wtnode *wtlist;   /* to list of weights to prev layer */
     struct unit *next;       /* link to next unit in this layer */
   } UNIT;

typedef struct wtnode
   {
     WTTYPE weight;           /* weight from here to backunit */
     WTTYPE olddw;            /* delta wji from previous iteration */
     WTTYPE total;            /* total of changes for batch updates */
     struct wtnode *next;     /* link to next node */
     UNIT *backunit;          /* ptr to unit the weight comes from */
   } WTNODE;

typedef struct layer
   {
     int unitcount;           /* number of units in this layer now */
     int initialcount;        /* number of units to start with */
     struct layer *backlayer; /* pointer to previous layer */
     struct layer *next;      /* pointer to next layer */
     UNIT *units;             /* start of list of units in this layer */
     PATLIST *patstart[2];    /* to the list of patterns */
     PATLIST *currentpat[2];  /* the current pattern */
   } LAYER;

typedef struct data
   {
    int npats;                /* number of patterns */
    INT32 iterno;             /* iteration number when data was found */
    int right;                /* number of patterns right */
    int wrong;                /* number of patterns wrong */
    float avgerr;             /* average abs error per unit */
    float pctright;           /* percentage right */
   } DATA;
